package com.snake.springboot.authority.service.assembler;

import com.snake.springboot.authority.api.model.AuthMenuModel;
import com.snake.springboot.authority.api.response.AuthMenuComplexRsp;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.dao.mybatis.entity.AuthMenuComplexDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthMenuDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthMenuTreeDO;
import org.apache.commons.collections.CollectionUtils;
import org.snake.common.code.SerialNumberGeneratorFactory;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类 {@code AuthMenuAssembler}  <br> .
 * <p>权限菜单组装器
 * 创建时间：2024/5/27 14:12
 *
 * @author Yzhui
 * @see
 */
public class AuthMenuAssembler {

    public static AuthMenuDO assembleAuthMenuDO(AuthMenuModel model, boolean isNew) throws Exception {
        AuthMenuDO entity = new AuthMenuDO();
        PropertyUtils.copyPropertiesIfNull(entity, model);
        if(isNew) {
            SerialNumberGeneratorFactory.buildId4Bean(entity, "menuId");
        }
        return entity;
    }

    public static AuthMenuModel assembleAuthMenuModel(AuthMenuComplexDO entity) {
        AuthMenuModel model = new AuthMenuModel();
        PropertyUtils.copyPropertiesIfNull(model, entity);
        return model;
    }

    public static AuthMenuComplexRsp assembleAuthMenuComplexRsp(AuthMenuComplexDO entity) {
        AuthMenuComplexRsp rsp = new AuthMenuComplexRsp();
        PropertyUtils.copyPropertiesIfNull(rsp, entity);
        return rsp;
    }


    public static List<AuthMenuRsp>formatTree(List<AuthMenuTreeDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<AuthMenuRsp> source = ListUtils.copy(list, AuthMenuRsp.class);
        List<AuthMenuRsp> tree= source.stream().filter(i -> i.getParentsId().equals("0")).collect(Collectors.toList());
        for (int i = 0;i < source.size(); i++) {
            AuthMenuRsp curr = source.get(i);
            List<AuthMenuRsp> children = source.stream().filter(j -> j.getParentsId().equals(curr.getMenuId())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(children)) {
               // List<AuthMenuRsp> targetChildren = ListUtils.copy(children, AuthMenuRsp.class);
                curr.setChildren(children);
            }
        }
        return tree;
    }



}
